/**
 *Use Git parameter plugin to Adds ability to choose branches,
 *tags or revisions from git repository configured in project.
 * Plugin：Git parameter
 */

pipeline {
    agent any
    parameters {
        gitParameter name: 'BRANCH_TAG',
                type: 'PT_BRANCH_TAG',
                branchFilter: 'origin/(.*)',
                defaultValue: 'master',
                selectedValue: 'DEFAULT',
                sortMode: 'DESCENDING_SMART',
                description: 'Select your branch or tag.'
        choice(name: 'SonarQube', choices: ['False','True'],description: '')
    }
    stages {
        stage('Example') {
            steps {
                checkout([$class: 'GitSCM',
                          branches: [[name: "${params.BRANCH_TAG}"]],
                          doGenerateSubmoduleConfigurations: false,
                          extensions: [],
                          gitTool: 'Default',
                          submoduleCfg: [],
                          userRemoteConfigs: [[url: 'https:/192.168.1.2/scm/test.git',credentialsId: 'for_gitlab',]]
                ])
            }
        }

        stage('QA Check') {
            when {
                expression { return env.sonarqube == "True" }
            }
            steps {
                withSonarQubeEnv('SonarQubeServer') {
                    sh ' $SonarScannerHome/bin/sonar-scanner ' +
                            '-Dsonar.sources=src/main ' +
                            '-Dsonar.projectKey="test" ' +
                            '-Dsonar.projectName="test" '
                }
                timeout(time: 30, unit: 'MINUTES') {
                    waitForQualityGate abortPipeline: true
                }
            }
        }
    }
}
